﻿using System;
using System.Collections;
using System.Xml;
using System.Text;
using SSTestAPI;

namespace Data_Storages
{
    public class DSXml : IDataStorage
    {
        private string Filepath { get; set; }
        
        public DSXml(string filepath)
        {
            Filepath = filepath;
        }

        public ArrayList Load()
        {
            var plantsList = new ArrayList();
            try
            {
                var xmlDoc = new XmlDocument();
                xmlDoc.Load(Filepath);
                if (xmlDoc.DocumentElement != null)
                {
                    var xmlNodeList = xmlDoc.DocumentElement.ChildNodes;
                    foreach (XmlNode xn in xmlNodeList)
                    {
                        if (xn.Attributes != null)
                            switch (xn.Attributes.GetNamedItem("PlantType").Value)
                            {
                                case "Flower":
                                    {
                                        plantsList.Add(new Flower(xn.Attributes.GetNamedItem("FlowerBranch").Value,
                                                                  Boolean.Parse(xn.Attributes.GetNamedItem("FlowerSymmetric").Value),
                                                                  Int32.Parse(xn.Attributes.GetNamedItem("FlowerLeafs").Value),
                                                                  Int32.Parse(xn.Attributes.GetNamedItem("FlowerLifetime").Value)));
                                        break;
                                    }
                                case "Tree":
                                    {
                                        plantsList.Add(new Tree(xn.Attributes.GetNamedItem("TreeBranch").Value,
                                                                xn.Attributes.GetNamedItem("TreeGroup").Value,
                                                                Boolean.Parse(xn.Attributes.GetNamedItem("TreeFruit").Value),
                                                                Int32.Parse(xn.Attributes.GetNamedItem("TreeLifetime").Value)));
                                        break;
                                    }
                                default:
                                    break;
                            }
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            return plantsList;
        }

        public void Save(ArrayList plantsList)
        {
            if (plantsList == null || plantsList.Count == 0)
            {
                throw new Exception("Error: empty list");
            }
            var xmlWriter = new XmlTextWriter(Filepath, Encoding.Unicode);
            xmlWriter.WriteStartDocument();
            xmlWriter.WriteStartElement("Plants");
            foreach (var ob in plantsList)
            {
                xmlWriter.WriteStartElement("Plant");
                if (ob is Tree)
                {
                    xmlWriter.WriteAttributeString("PlantType", "Tree");
                    xmlWriter.WriteAttributeString("TreeBranch", ((Tree)ob).Branch);
                    xmlWriter.WriteAttributeString("TreeGroup", ((Tree)ob).Group);
                    xmlWriter.WriteAttributeString("TreeFruit", ((Tree)ob).Fruit.ToString());
                    xmlWriter.WriteAttributeString("TreeLifetime", ((Tree)ob).getLifetime().ToString());
                }
                else if (ob is Flower)
                {
                    xmlWriter.WriteAttributeString("PlantType", "Flower");
                    xmlWriter.WriteAttributeString("FlowerBranch", ((Flower)ob).Branch);
                    xmlWriter.WriteAttributeString("FlowerSymmetric", ((Flower)ob).Symmetric.ToString());
                    xmlWriter.WriteAttributeString("FlowerLeafs", ((Flower)ob).LeafsQuantity.ToString());
                    xmlWriter.WriteAttributeString("FlowerLifetime", ((Flower)ob).getLifetime().ToString());
                }
                xmlWriter.WriteEndElement();
            }
            xmlWriter.WriteEndElement();
            xmlWriter.WriteEndDocument();
            xmlWriter.Close();
        }
    }
}
